#pragma once
#include "Log.hxx"
#include <string>
#include <vector>
#include <memory>
#include <array>
#include <sys/select.h>
#include <algorithm>
#include <sys/time.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unordered_map>
#include <functional>
#include "Common.h"


class Connection;
class EpollServer;



class Connection
{
public:
    EpollServer *_R;
    using _func_t = std::function<void(std::shared_ptr<Connection>)>;

private:
    int _sockfd;
    std::string _ip;
    uint16_t _port;
    uint32_t _events;
    
    _func_t _recver = nullptr;
    _func_t _sender = nullptr;
    _func_t _excepter = nullptr;

public:
    std::vector<char> _read_buf;
    std::vector<char> _write_buf;
    // 回溯体，简单理解就是子节点便于找到父亲的那条线
    Connection(int sockfd,
                uint32_t events,
                EpollServer *R)
        : _sockfd(sockfd), _events(events), _R(R)
    {
    }
    void RegisterFunc(_func_t recv, _func_t send, _func_t excepter)
    {
        _recver = recv;
        _sender = send;
        _excepter = excepter;
    }
    void implementRecv(std::shared_ptr<Connection> Mythis)
    {
        if (_recver == nullptr)
        {
            myService::lg(myService::Log::Error, "recv func is nullptr，收取函数未初始化，直接退出\n");
            return;
        }
        _recver(Mythis);
    }
    void implementSend(std::shared_ptr<Connection> Mythis)
    {
        if (_sender == nullptr)
        {
            myService::lg(myService::Log::Error, "send func is nullptr，发送函数未初始化，直接退出\n");
            return;
        }
        _sender(Mythis);
    }
    void implementExcepter(std::shared_ptr<Connection> Mythis)
    {
        if (_excepter == nullptr)
        {
            myService::lg(myService::Log::Error, "excepter func is nullptr，异常处理函数未初始化，直接退出\n");
            return;
        }
        _excepter(Mythis);
    }
    //该接口仅仅是修改了存储的值，没有添加到epoller之中
    void SetEvents(uint32_t events)
    {
        _events = events;
    }
    int GetFd()
    {
        return _sockfd;
    }
    uint32_t GetEvents()
    {
        return _events;
    }
    std::string GetClientIp()
    {
        return _ip;
    }
    uint16_t GetClientPort()
    {
        return _port;
    }
    ~Connection()
    {
        close(_sockfd);
    }
};
// 在外部提供好SockFd以及Sockfd的属性(比如非阻塞等),交由Maker设计链接
class ConnectionMaker
{
public:
    static std::shared_ptr<Connection> BuildListenConnection(int sockfd, EpollServer *R, uint32_t events, Connection::_func_t recver)
    {
        std::shared_ptr<Connection> ptr(new Connection(sockfd, events, R));
        ptr->RegisterFunc(recver, nullptr, nullptr);
        return ptr;
    }
    static std::shared_ptr<Connection> BuildNormalConnection(int sockfd,
                                                                EpollServer *R,
                                                                uint32_t events,
                                                                Connection::_func_t recver,
                                                                Connection::_func_t sender,
                                                                Connection::_func_t excepter)
    {
        std::shared_ptr<Connection> ptr(new Connection(sockfd, events, R));
        ptr->RegisterFunc(recver, sender, excepter);
        return ptr;
    }
};  